//
//  XGardenViewController.swift
//  WeiBoReDo
//
//  Created by 放学跟鸽走 on 2017/4/15.
//  Copyright © 2017年 放学跟鸽走. All rights reserved.
//

import UIKit
import SVProgressHUD
private let normalCellId = "normalCellId"
private let bigPicCellId = "bigPicCellId"
private let picsCellId = "picsCellId"
class XGardenViewController: XBaseController {
    lazy var newsListViewModel:XGardenNewsListViewModel = XGardenNewsListViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupTableView()
        
        
    }
    override func loadData() {
        
        newsListViewModel.getGardenNews(isPull: false) { (isSuccess) in
            printLog(message: "load more isSuccess \(isSuccess)")
            if isSuccess{
                self.myTableView?.reloadData()
                self.tableReFreshCtr.endRefreshing()
            }
        }
    }
    override func loadMore() {
        SVProgressHUD.showInfo(withStatus: "正在加载中")
        newsListViewModel.getGardenNews(isPull: true) { (isSuccess) in
            printLog(message: "load more isSuccess \(isSuccess)")
            if isSuccess{
                //收新列表
                self.myTableView?.reloadData()
                //把上拉刷新的锁打开
                self.isPullUp = false
            }
        }
    }
   
    

}
// MARK: - UI搭建
extension XGardenViewController{
    func setupTableView(){
        myTableView?.register(XGardenNewsNormalCell.self, forCellReuseIdentifier: normalCellId)
        myTableView?.register(XGardenNewsBigImgCell.self, forCellReuseIdentifier: bigPicCellId)
        myTableView?.register(XGardenNewsPicsCell.self, forCellReuseIdentifier: picsCellId)
        myTableView?.separatorStyle = .none
    }

}
// MARK: - tableView数据源及代理方法
extension XGardenViewController{
    //MARK: TableView DATA SOURCE
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return newsListViewModel.modelList.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let viewModel = newsListViewModel.modelList[indexPath.row]
        var cellId = normalCellId
//        print("\(viewModel.gardenNews?.title) =\(viewModel.gardenNews?.app_flag)")
        if viewModel.gardenNews?.cellType == XGardenNewsCellType.BigPic{
            cellId = bigPicCellId
            let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as? XGardenNewsBigImgCell
            cell?.viewModel = viewModel
            return cell ?? XGardenNewsBigImgCell()
        }else if viewModel.gardenNews?.cellType == XGardenNewsCellType.ThreePic{
            cellId = picsCellId
            let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as? XGardenNewsPicsCell
            cell?.viewModel = viewModel
            return cell ?? XGardenNewsPicsCell()
        
        }
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as? XGardenNewsNormalCell
        cell?.viewModel = viewModel
        return cell ?? XGardenNewsNormalCell()
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return newsListViewModel.modelList[indexPath.row].rowHeight
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let vc = XWebViewController()
        let viewModel = newsListViewModel.modelList[indexPath.row]
        vc.urlString = viewModel.gardenNews?.link
        
        navigationController?.pushViewController(vc, animated: true)
    }
}

